//
//  PlaceholderTextView.m
//  CMJWeibo
//
//  Created by BH on 16/1/6.
//
//

#import "PlaceholderTextView.h"
#import "UIView+MJ.h"

@interface PlaceholderTextView ()

@property(nonatomic,strong)UILabel *placeholderLab;

@end

@implementation PlaceholderTextView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *placeLab = [[UILabel alloc] init];
        placeLab.x = 5;
        placeLab.y = 5;
        placeLab.textColor = [UIColor lightGrayColor];
        placeLab.font = self.font;
        placeLab.numberOfLines = 0;
        [self addSubview:placeLab];
        self.placeholderLab = placeLab;
        
        //监听文字改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 *  监听文字改变
 */
-(void)textChange
{
    if (self.placeholder.length) {
        //开始编辑
        self.placeholderLab.hidden = YES;
    }else{
        self.placeholderLab.hidden = NO;
    }
}

-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    
    self.placeholderLab.text = placeholder;
    CGSize labelMaxSize = CGSizeMake(self.width, MAXFLOAT);
    CGSize labelSize = [placeholder sizeWithFont:self.placeholderLab.font constrainedToSize:labelMaxSize];
    self.placeholderLab.size = labelSize;
}

-(void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    
    self.placeholderLab.text = placeholderColor;
}

-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    
    self.placeholderLab.font = font;
}

@end
